//
//  ViewController.m
//  XOR_Decrypt
//
//  Created by apple on 2019/12/2.
//  Copyright © 2019 apple. All rights reserved.
//

#import "ViewController.h"
#include <CommonCrypto/CommonDigest.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 加密Key
    NSString *secretKey = [self md5:@"346"];
    
    NSString *string = [self decodeWithFile:@"fw_cdn.txt" secretKey:secretKey];
    NSLog(@"string = %@",string);
    
    NSString *string1 = [self decodeWithFile:@"fw_desc.json" secretKey:secretKey];
    NSLog(@"string1 = %@",string1);
    
    UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    imgview.image = [self imageWithFile:@"fw_lock.png" secretKey:secretKey];
    [self.view addSubview:imgview];
}

- (UIImage *)imageWithFile:(NSString *)fileName  secretKey:(NSString *)secretKey {
    NSURL *bundleUrl = [[NSBundle mainBundle] URLForResource:@"Resource" withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:bundleUrl];
    NSString *imagePath = [bundle.resourcePath stringByAppendingPathComponent:fileName];
    //NSLog(@"imagePath = %@",imagePath);
    
    // 读取被加密文件对应的数据
    NSData *encryptData = [NSData dataWithContentsOfFile:imagePath];

    // 对数据进行XOR解密
    NSData *decryptData = [self xor_encrypt:encryptData secretKey:secretKey];
    return [UIImage imageWithData:decryptData];
}


- (NSString *)decodeWithFile:(NSString *)fileName  secretKey:(NSString *)secretKey {
    NSURL *bundleUrl = [[NSBundle mainBundle] URLForResource:@"Resource" withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:bundleUrl];
    NSString *imagePath = [bundle.resourcePath stringByAppendingPathComponent:fileName];
    //NSLog(@"imagePath = %@",imagePath);
    
    // 读取被加密文件的数据
    NSData *encryptData = [NSData dataWithContentsOfFile:imagePath];
    
    // 对数据进行XOR解密
    NSData *decryptData = [self xor_encrypt:encryptData secretKey:secretKey];
    NSString *decryptJson = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
    //NSLog(@"decryptJson = %@",decryptJson);
    
    return decryptJson;
}

#pragma mark - Tool
/// MD5加密
- (NSString *)md5:(NSString *)text {
    const char *cStr = [text UTF8String];
    unsigned char result[16];
    
    CC_MD5(cStr, (unsigned char)strlen(cStr), result );
    NSString *md5Str = [NSString stringWithFormat:
                        @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                        result[0], result[1], result[2], result[3],
                        result[4], result[5], result[6], result[7],
                        result[8], result[9], result[10], result[11],
                        result[12], result[13], result[14], result[15]
                        ];
    return [md5Str lowercaseString];
}

/// 异或加密
- (NSData *)xor_encrypt:(NSData*)data secretKey:(NSString *)secretKey {
    // 获取key的长度
    NSInteger length = secretKey.length;
    // 将OC字符串转换为C字符串
    const char *keys = [secretKey cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char cKey[length];
    //memcpy函数的功能是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中
    memcpy(cKey, keys, length);
    // 数据初始化，空间未分配 配合使用 appendBytes
    NSMutableData *encryptData = [[NSMutableData alloc] initWithCapacity:length];
    // 获取字节指针
    const Byte *point = data.bytes;
    for (int i = 0; i < data.length; i++) {
        // 算出当前位置字节，要和密钥的异或运算的密钥字节
        int l = i % length;
        char c = cKey[l];
        // 异或运算
        Byte b = (Byte) ((point[i]) ^ c);
        // 追加字节
        [encryptData appendBytes:&b length:1];
    }
    return encryptData.copy;
}


@end
